package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * Created by 80002023 on 2016/7/26.
 */
public class AnimationPlayer extends Actor {
    private Pixmap mPixmap;
    private Texture mTexture;
    private TextureRegion[] keyFrames;
    private Animation mAnimation;
    private float stateTime;

    public AnimationPlayer() {

        mPixmap = new Pixmap(Gdx.files.internal("gremlins.png"));
        mTexture = new Texture(mPixmap);
        keyFrames = new TextureRegion[3];
        keyFrames[0] = new TextureRegion(mTexture, 0, 0, 30, 30);
        keyFrames[1] = new TextureRegion(mTexture, 30, 0, 30, 30);
        keyFrames[2] = new TextureRegion(mTexture, 60, 0, 30, 30);
        mAnimation = new Animation((float) 0.2, keyFrames);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

        stateTime += Gdx.graphics.getDeltaTime();

        TextureRegion textureRegion = mAnimation.getKeyFrame(stateTime, true);
        // 这里要注意，我们添加的action只是改变actor的属性值，绘制的时候并没有
        // 自动给我们处理这些逻辑， 我们要做的就是取得这些值，然后自己处理
        batch.draw(textureRegion, getX(), getY(),
                textureRegion.getRegionWidth() / 2,
                textureRegion.getRegionHeight() / 2,
                textureRegion.getRegionWidth(),
                textureRegion.getRegionHeight(), getScaleX(), getScaleY(),
                getRotation());
    }

    @Override
    public void clear() {

        super.clear();
        mPixmap.dispose();
        mTexture.dispose();
    }
}
